<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>${cfg.tableComment}
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <#if cfg.searchList??>
                <#list cfg.searchList as search>
                <el-input v-model="query.${search.columnName}" placeholder="${search.columnComment}" class="filter-item handle-input mr10"></el-input>
                </#list>
                </#if>
                <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-plus" class="filter-item" @click="handleAdd">新增</el-button>
            </div>
            <el-table
                    v-loading="loading"
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <#list cfg.tableList as table>
                <#if table.type == '1' || table.type == '2'>
                <el-table-column prop="${table.columnName}" label="${table.columnComment}" align="center"></el-table-column>
                <#elseif table.type == '3'>
                <el-table-column label="${table.columnComment}" align="center">
                    <template slot-scope="scope">
                        <el-image
                                class="table-td-thumb"
                                :src="scope.row.${table.columnName}"
                                :preview-src-list="[scope.row.${table.columnName}]"
                        ></el-image>
                    </template>
                </el-table-column>
                <#elseif table.type == '4'>
                <el-table-column prop="${table.columnName}" label="${table.columnComment}" width="180">
                    <template slot-scope="scope">
                        <span>{{ parseDateTime(scope.row.${table.columnName}) }}</span>
                    </template>
                </el-table-column>
                </#if>
                </#list>
                <el-table-column label="操作" width="220" align="center">
                    <template slot-scope="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                                type="text"
                                icon="el-icon-delete"
                                class="red"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.current"
                    :page-size="query.size"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="50%">
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <#list cfg.formList as table>
                <#if table.columnName != "id" && (table.type == '1' || table.type == '2')>
                <el-form-item label="${table.columnComment}" prop="${table.columnName}" >
                    <el-input v-model="form.${table.columnName}"></el-input>
                </el-form-item>
                <#elseif table.columnName != "id" && table.type == '3'>
                <el-form-item label="${table.columnComment}" prop="${table.columnName}">
                    <el-input v-model.number="form.${table.columnName}"></el-input>
                </el-form-item>
                <#elseif table.columnName != "id" && table.type == '4'>
                <el-form-item label="${table.columnComment}">
                    <el-upload
                            ref="upload"
                            class="avatar-uploader"
                            action=""
                            :http-request="fileUpload"
                            :show-file-list="false"
                            :before-upload="beforeAvatarUpload">
                        <img v-if="form.${table.columnName}" :src="form.${table.columnName}" class="avatar">
                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                    </el-upload>
                </el-form-item>
                </#if>
                </#list>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" :disabled="formLoading" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { ${cfg.entityName}Page,${cfg.entityName}Save,${cfg.entityName}Update,${cfg.entityName}Delete } from '../../../api/${cfg.moduleName}/${cfg.entityName}';

export default {
    name: '<#if cfg.splitArgs??><#list cfg.splitArgs as splitArg><#if splitArg_index = 0>${splitArg}<#else>-${splitArg}</#if></#list></#if>-list',
    data() {
        return {
            // 遮罩层
            loading: true,
            formLoading: false,
            query: {
                <#if cfg.searchList??>
                <#list cfg.searchList as search>
                ${search.columnName}:'',
                </#list>
                </#if>
                current: 1,
                size: 10
            },
            editVisible:false,
            form:{
                <#list cfg.formList as table>
                ${table.columnName}:"",
                </#list>
            },
            tableData: [],
            pageTotal: 0,
            rules: {
                <#list cfg.formList as table>
                ${table.columnName}: [
                    { required: true, message: '请输入${table.columnComment}', trigger: 'blur' }
                ],
                </#list>
            }
        };
    },
    created() {
        this.defaultform = JSON.parse(JSON.stringify(this.form));
        this.getData();
    },
    destroyed() {
        this.defaultform = null;
    },
    methods: {
        getData() {
            this.loading = true;
            ${cfg.entityName}Page(this.query).then(res => {
                this.loading = false;
                this.tableData = res.data;
                this.pageTotal = res.count;
            });
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    ${cfg.entityName}Delete(row.id).then(res => {
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`删除成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                })
                .catch(() => {});
        },
        handleAdd(){
            this.form = Object.assign({}, this.defaultform);
            this.editVisible = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        handleEdit(index, row) {
            <#list cfg.formList as table>
            this.form.${table.columnName} = row.${table.columnName};
            </#list>
            this.editVisible = true;
            this.$nextTick(()=>{
                this.$refs['form'].clearValidate();
            })
        },
        // 保存编辑
        saveEdit() {
            this.$refs['form'].validate((valid) => {
                if (!valid) {
                    console.log('error submit!!');
                    return false;
                }
                this.formLoading = true;
                if (this.form.id) {
                    ${cfg.entityName}Update(this.form).then(res => {
                        this.formLoading = false;
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`修改成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                } else {
                    ${cfg.entityName}Save(this.form).then(res => {
                        this.formLoading = false;
                        if (res.code == 0) {
                            this.editVisible = false;
                            this.$message.success(`修改成功`);
                            this.getData();
                        } else {
                            this.$message.error(res.msg);
                            return false;
                        }
                    });
                }
            });
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'current', 1);
            this.getData();
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'current', val);
            this.getData();
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
